package pr.iceworld.fernando.springbatchquartzlearning.configuration;


import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component("springQuartzJob")
public class SpringQuartzJob extends QuartzJobBean {
    @Resource
    Job dataHandleJob;
    @Autowired
    private JobLauncher jobLauncher;
    @Override
    public void executeInternal(final JobExecutionContext context) throws JobExecutionException {
        System.out.println("TestJobStart:" + Thread.currentThread().getId());
        try {
            init();
            JobParameters jobParameters= new JobParametersBuilder().addLong("time", System.currentTimeMillis())
                    .toJobParameters();
            JobExecution result =jobLauncher.run(dataHandleJob, jobParameters);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Job1End");
    }

    public void init() {
        dataHandleJob =(Job) MyApplicationContextUtil.getBeanObj("dataHandleJob");
        jobLauncher =(JobLauncher) MyApplicationContextUtil.getBeanObj("jobLauncher",JobLauncher.class);
    }
}